const log = require("../utils/log");
const game_config = require("../config/game_config");
const redisBase = require('./RedisBase');

class Common {
    static getInstance() {
        if (!Common.instance) {
            Common.instance = new Common();
            return Common.instance;
        } else {
            return Common.instance;
        }
    }

    constructor() {

    }

    //清空redis 里面列表 room_list_zts 的主题赛 房间数据
    async clearRs_roomListZt(uid){
        log.info('=========clearRs_roomListZt============',uid);

        let rList = await redisBase.getValidRoomInList();
        for (let i = 0; i < rList.length; i++) {
            let cfg = JSON.parse(rList[i]);
            let idx = cfg.players.indexOf(uid);
            if(idx !== -1){
                if(idx === 0){ //我创建的房间
                    //解散房间
                    log.info('===========clearRs_roomListZt end===我创建的房间==============');
                    await redisBase.delRooomList(i, JSON.stringify(cfg));
                }else if(idx === 1){//我加入的房间
                    log.info('===========clearRs_roomListZt end===我加入的房间==============');
                    cfg.players[idx] = null;
                    await redisBase.updateRoomListValue(i, JSON.stringify(cfg));
                }
                break
            }
        }
        log.info('===========clearRs_roomListZt end=================');
    }

    //设置列表 gameInfo_list_zt 的房间号
    async set_gameInfo_list_zt_roomId(uid, gType, roomId = 0){
        let gList = await redisBase.getGameListZt();
        for (let i = 0; i < gList.length; i++) {
            let cfg = JSON.parse(gList[i]);
            if(cfg.uid === uid && cfg.gamType === gType){
                cfg.roomId = roomId;
                log.info("==========set_gameInfo_list_zt_rromId=========", cfg.roomId,cfg.uid)
                await redisBase.updateGameListTzValue(i,JSON.stringify(cfg));
                break;
            }
        }
    }
}

module.exports = Common.getInstance();